/********************************
 ***   Image3DenaimcRange.c   ***
 ********************************/

#include "ImageType/ImageType.h"
#include "ImageType/ImageTool.h"


int	image1_histogram_percent( image_type *im, float pT );

void	image1_dynamic_range( image_type *im, float h0, float h1 );



void
image1_histogram( image_type *im, int h[] )
{
	u_char	*sp;
	int	i,	j;




	for( i = 0 ; i < 256 ; i++ )	h[i] = 0;

	sp = (u_char *)im->data;
	for( i = 0 ; i < im->row ; i++ )
		for( j = 0 ; j < im->column ; j++ ){
			h[*sp++] ++;
		}
}

void
image1M_histogram( image_type *im, image_type *mim, int h[] )
{
u_char	*sp,	*mp;
int	i,	j;




	for( i = 0 ; i < 256 ; i++ )	h[i] = 0;

	sp = im->data;
	mp = mim->data;
	for( i = 0 ; i < im->row ; i++ )
		for( j = 0 ; j < im->column ; j++, sp++, mp++ ){
			if( *mp != 0 )
				h[*sp] ++;
		}
}


void
image1_dynamic_range( image_type *im, float h0, float h1 )
{
u_char	*sp;
int	i,	j;
int	h[256];
int	sum,	max,	tmp;
float	a0,	a1,	a,	b;

	
	

	for( i = 0 ; i < 256 ; i++ )	h[i] = 0;

	sp = (u_char *)im->data;
	for( i = 0 ; i < im->row ; i++ )
		for( j = 0 ; j < im->column ; j++ ){
			h[*sp++] ++;
		}

	max = im->row *im->column * h0/100;
	for( i = 0, sum = 0 ; i < 256 ; i++ ){
		sum += h[i];
		if( sum > max )	break;
	}
	a0 = i -1;

	max = im->row *im->column * h1/100;
	for( i = 255, sum = 0 ; i > 0 ; i-- ){
		sum += h[i];
		if( sum > max )	break;
	}
	a1 = i + 1;


	a = 255/(a1-a0);
	b = - a* a0;

	

	for( i = 0 ; i < 256 ; i++ ){
		tmp = a * i + b;
		h[i] = PUSH_TO_RANGE( tmp, 0, 255 );
	}
	

	sp = (u_char *)im->data;
	for( i = 0 ; i < im->row ; i++ )
		for( j = 0 ; j < im->column ; j++ ){
			*sp = h[ *sp];
			sp++;
		}
}


void
image1_dynamic_range_R( image_type *im, box2i *box, float h0, float h1 )
{
int	h[256];
u_char	*sp;
int	i,	j;
int	tmp;
float	a0,	a1,	a,	b;
float	m1,	m0;

	image1_dynamic_rangeH_R( im, box, h0, h1, &a0, &a1 );

	m1 = MAX( a1, 245 );
	m0 = MIN( a0, 15 );

	
	//a = 255/(a1-a0);
	//b = - a* a0;

	a = ( m1 - m0 )/( a1-a0);
	b = m0 - a*a0;



	for( i = 0 ; i < 256 ; i++ ){
		tmp = a * i + b;
		h[i] = PUSH_TO_RANGE( tmp, 0, 255 );
	}


	sp = (u_char *)im->data;
	for( i = 0 ; i < im->row ; i++ )
		for( j = 0 ; j < im->column ; j++ ){
			*sp = h[ *sp];
			sp++;
		}
}



void
image1_dynamic_rangeH_R( image_type *im, box2i *box, float h0, float h1, float *a0, float *a1 )
{
u_char	*sp;
int	i,	j,	i0,	i1,	j0,	j1,	n;
int	h[256];
int	sum,	max;

	imageA_box( im ,box, &j0, &i0, &j1, &i1 );

	for( i = 0 ; i < 256 ; i++ )	h[i] = 0;


	for( i = i0, n = 0 ; i <= i1 ; i++ ){
		sp = IMAGE_PIXEL( im, i, j0 );
		for( j = j0 ; j <= j1 ; j++ ){
			h[*sp++] ++;
			n++;
		}
	}




	max = n * h0/100;
	for( i = 0, sum = 0 ; i < 256 ; i++ ){
		sum += h[i];
		if( sum > max )	break;
	}
	*a0 = i -1;



	max = n * h1/100;
	for( i = 255, sum = 0 ; i > 0 ; i-- ){
		sum += h[i];
		if( sum > max )	break;
	}
	*a1 = (i == 255)? 255 : i + 1;
}




int
image1_histogram_percent( image_type *im, float pT )
{
u_char	*sp;
int	i,	j;
int	sum,	h[256];
float	T;



	for( i = 0 ; i < 256 ; i++ )	h[i] = 0;

	sp = (u_char *)im->data;
	for( i = 0 ; i < im->row ; i++ )
		for( j = 0 ; j < im->column ; j++ ){
			h[*sp++] ++;
		}




	T = im->row *im->column * pT/100;
	for( i = 0, sum = 0 ; i < 256 ; i++ ){
		sum += h[i];
		if( sum > T )	break;

	}

	return( i );
}



int
image1M_histogram_percent( image_type *im, image_type *mim, float pT )
{
u_char	*sp,	*mp;
int	i,	j,	no;
int	sum,	h[256];
float	T;



	for( i = 0 ; i < 256 ; i++ )	h[i] = 0;

	sp = (u_char *)im->data;
	mp = (u_char *)mim->data;
	for( i = 0, no = 0 ; i < im->row ; i++ )
		for( j = 0 ; j < im->column ; j++, sp++, mp++ ){
			if( *mp == 1 ){
				h[*sp] ++;
				no++;
			}
		}




		T = no * pT/100;
		for( i = 0, sum = 0 ; i < 256 ; i++ ){
			sum += h[i];
			if( sum > T )	break;

		}

		return( i );
}




int
image1M_histogram_range( image_type *im, image_type *mim, int nRange, int range[] )
{
u_char	*sp,	*mp;
int	i,	j,	no,	n;
int	sum,	h[256];
float	T;



	for( i = 0 ; i < 256 ; i++ )	h[i] = 0;

	sp = (u_char *)im->data;
	mp = (u_char *)mim->data;
	for( i = 0, no = 0 ; i < im->row ; i++ )
		for( j = 0 ; j < im->column ; j++, sp++, mp++ ){
			if( *mp == 1 ){
				h[*sp] ++;
				no++;
			}
		}



	T = no / nRange;

	
	for( i = 0, sum = 0, n = 0 ; i < 256 ; i++ ){
		sum += h[i];
		if( sum > T ){
			range[n++] = i;
			sum = 0;
		}
	}

	if( sum > 0.5*T )
		range[n++] = i;

	return( n );
}
